import {
  KfGroupPicker,
  KfRolePicker,
  KfBackendEnumPicker,
  KfBizQuickFormProps,
  KfUpload,
  KfFloatingContextProps,
  KfInput,
  KfInputPassword,
  KfQuickForm,
  KfTextArea,
  useBackendEnum,
  DEFAULT_FORM_LAYOUT,
  KfBackendDictPicker, KfMapPointPicker
} from "kf-web-library";
import * as service from '@/services/system/user/UserService';
import {Divider, Form} from 'antd';
import React, {useState} from 'react';
import * as API from "@/services";

const FormItem = Form.Item;
type QueryEntity = API.User;
type CreateEntity = API.User_Create;
type ModifyEntity = API.User_FullModify;

type DataFormProps = {} & KfBizQuickFormProps & KfFloatingContextProps;

/**
 * 用户表单
 */
const QuickForm: React.FC<DataFormProps> = (props) => {
  const [bizData, setBizData] = useState<QueryEntity | null>(null);

  const {enumDict, getBackendEnum} = useBackendEnum();

  const {formType, bizId, ...rest} = props;
  //是否修改当前登录用户
  const isModifyLoginedSelf = formType === 'modify' && !bizId;
  //是否创建
  const isCreate = formType === 'create';

  const isOp = false; //hasAnyAuthority("ROLE_BASE_ADMIN") && !hasAnyAuthority("ROLE_SUPER_ADMIN")

  return (
    <KfQuickForm
      formType={formType}
      formProps={{
        ...DEFAULT_FORM_LAYOUT,
      }}
      defaultQueryRequest={() => {
        let processPromise;
        if (!isModifyLoginedSelf) {
          processPromise = service.queryUser(bizId!);
        } else {
          processPromise = service.queryCurrentUser();
        }
        return processPromise.then((resp) => {
          if (resp.flag) {
            resp.data = {
              ...resp.data,
              roleIds: resp.data.roles?.map((r) => r.id) || [],
              groupIds: resp.data.groups?.map((g) => g.id) || [],
            } as ModifyEntity;
            setBizData(resp.data);
          }
          return new Promise((resolve) => resolve(resp));
        });
      }}
      submitRequest={{
        create: (formValues) => service.createUser({
          ...formValues,
          type: "CONSUMER",
        } as CreateEntity),
        modify: (formValues) => {
          return service.modifyUser({
            ...formValues,
            id: bizData?.id,
          } as ModifyEntity);
        },
      }}
      {...rest}
    >
      {props.formType === 'create' && (
        <>
          <FormItem name={nameof<CreateEntity>((x) => x.identifier)} label="登录账号" rules={[{required: true}]}>
            <KfInput placeholder={'用户名/手机号/邮箱'}/>
          </FormItem>
          <FormItem name={nameof<CreateEntity>((x) => x.credential)} label="登录密码" rules={[{required: true, min: 6}]}>
            <KfInputPassword/>
          </FormItem>
          <Divider/>
        </>
      )}
      <FormItem name={nameof<ModifyEntity>((x) => x.avatar)} label="头像" rules={[{required: false, message: '请上传头像!'}]}>
        <KfUpload.Avatar readonly={isOp}/>
      </FormItem>

      <FormItem name={nameof<ModifyEntity>((x) => x.nickname)} label="用户昵称" rules={[{required: true}]}>
        <KfInput readonly={isOp}/>
      </FormItem>

      <FormItem name={nameof<ModifyEntity>((x) => x.description)} label="备注" rules={[{required: false}]}>
        <KfTextArea readonly={isOp}/>
      </FormItem>
    </KfQuickForm>
  );
};

export default QuickForm;
